package com.ptu.controller.user;

import com.ptu.dto.AddressDTO;
import com.ptu.dto.AddressUpdateDTO;
import com.ptu.entity.Address;
import com.ptu.server.AddressServer;
import com.ptu.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "地址模块")
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressServer addressServer;

    @Operation(summary = "获取地址信息")
    @GetMapping("/list")
    public List<Address> getAddress() {
        System.out.println("获取地址信息");
        List<Address> list = addressServer.list();
        return list;
    }

    @Operation(summary = "添加地址信息")
    @PostMapping("/add")
    public Result<Void> addAddress(@RequestBody AddressDTO addressDTO) {
        System.out.println("添加地址信息");
        System.out.println(addressDTO);
        addressServer.add(addressDTO);
        return Result.success();
    }

    @Operation(summary = "删除地址信息")
    @DeleteMapping("/delete")
    public Result<Void> deleteAddress(Integer id) {
        System.out.println("删除地址信息");
        addressServer.delete(id);
        return Result.success();
    }

    @Operation(summary = "修改地址信息")
    @PostMapping("/update")
    public Result<Void> updateAddress(@RequestBody AddressUpdateDTO addressUpdateDTO) {
        System.out.println("修改地址信息");
        addressServer.update(addressUpdateDTO);
        return Result.success();
    }

    @Operation(summary = "设置默认地址")
    @PostMapping("/setDefault")
    public Result<Void> setDefault(Integer id) {
        System.out.println("设置默认地址");
        addressServer.setDefault(id);
        return Result.success();
    }

    @Operation(summary = "获取默认地址")
    @GetMapping("/default")
    public Result<Address> getDefault() {
        System.out.println("获取默认地址");
        Address address = addressServer.getDefault();
        System.out.println(address);
        return Result.success(address);
    }


}
